package com.eurasia.assignment.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.eurasia.assignment.domain.Assignment;
import com.eurasia.assignment.service.AssignmentService;
import com.eurasia.assignment.controller.dto.AssignmentRequest;
import com.eurasia.assignment.controller.dto.AssignmentResponse;
import com.eurasia.assignment.controller.dto.AssignmentSearchRequest;
import com.eurasia.assignment.controller.dto.PageResponse;

import lombok.Data;

@RestController
@RequestMapping("/assignments")
public class AssignmentController {
    private final AssignmentService assignmentService;
    private static final int DEFAULT_PAGE_SIZE = 4;

    public AssignmentController(AssignmentService assignmentService) {
        this.assignmentService = assignmentService;
    }

    @GetMapping("/{id}")
    public ResponseEntity<AssignmentResponse> getAssignment(@PathVariable Long id) {
        return assignmentService.getAssignment(id)
                .map(assignment -> ResponseEntity.ok(AssignmentResponse.fromDomain(assignment)))
                .orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("")
    public Object getAllAssignments(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String course,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer page) {
        
        AssignmentSearchRequest searchRequest = new AssignmentSearchRequest();
        searchRequest.setName(name);
        searchRequest.setCourse(course);
        searchRequest.setType(type);
        searchRequest.setStatus(status);
        
        // 如果指定了页码，返回分页结果
        if (page != null) {
            var pageResult = assignmentService.searchAssignmentsWithPagination(searchRequest, page, DEFAULT_PAGE_SIZE);
            List<AssignmentResponse> content = pageResult.getContent().stream()
                    .map(AssignmentResponse::fromDomain)
                    .collect(Collectors.toList());
                    
            return PageResponse.of(
                content,
                pageResult.getNumber(),
                pageResult.getSize(),
                pageResult.getTotalElements()
            );
        }
        
        // 否则返回所有结果（保持向后兼容）
        return assignmentService.searchAssignments(searchRequest).stream()
                .map(AssignmentResponse::fromDomain)
                .collect(Collectors.toList());
    }
    
    @PostMapping("")
    public AssignmentResponse createAssignment(@RequestBody AssignmentRequest request) {
        Assignment assignment = Assignment.create(
            request.getName(),
            request.getCourse(),
            request.getType()
        );
        Assignment createdAssignment = assignmentService.createAssignment(assignment);
        return AssignmentResponse.fromDomain(createdAssignment);
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAssignment(@PathVariable Long id) {
        boolean deleted = assignmentService.deleteAssignment(id);
        return deleted ? ResponseEntity.ok().build() : ResponseEntity.notFound().build();
    }

    @Data
    public static class CreateAssignmentRequest {
        private String name;
        private String course;
        private String type;
    }
} 